package com.my.study.designPatterns.proxyPattern.dynamicProxy;

import java.lang.reflect.Proxy;

public class MatchMakingTestDrive {

    public static void main(String[] args) {


        MatchMakingTestDrive drive = new MatchMakingTestDrive();
        drive.drive();
    }


    public MatchMakingTestDrive() {
    }


    public void drive(){

        PersonBean personBean = new PersonBeanImpl("John", "male", "Singing, Dancing, Painting", 40, 20);

        PersonBean ownerProxy = getOwnerProxy(personBean);
        try {
            System.out.println(ownerProxy.getName() + "====" + ownerProxy.getHotOrNotRating());
            ownerProxy.setHotOrRating(123);
        }catch (Exception e){
            e.printStackTrace();
        }


        PersonBean nonOwnerProxy = getNonOwnerProxy(personBean);
        try{
            System.out.println(nonOwnerProxy.getName() + "--------getHotOrNotRating is: " + nonOwnerProxy.getHotOrNotRating());
            nonOwnerProxy.setName("Marry");

        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public PersonBean getNonOwnerProxy(PersonBean personBean){

        return (PersonBean) Proxy.newProxyInstance(
                personBean.getClass().getClassLoader(),
                personBean.getClass().getInterfaces(),
                new NoOwnerInvocationHandler(personBean));
    }

    public PersonBean getOwnerProxy(PersonBean personBean){

        return (PersonBean) Proxy.newProxyInstance(
                personBean.getClass().getClassLoader(),
                personBean.getClass().getInterfaces(),
                new OwnerInvocationHandler(personBean));

    }



}
